package kama;

import java.util.Arrays;
import java.util.Scanner;

public class Test0053 {
}

class Main0053_1{
    public static void main(String[] args) {
        int ans=0;

        Scanner scanner = new Scanner(System.in);
        String[] split = scanner.nextLine().split(" ");

        int v = Integer.parseInt(split[0]);
        int e = Integer.parseInt(split[1]);

        int[][] graph = new int[v+1][v+1];
        for (int i = 1; i <= v; i++) {
            for (int j = 1; j <= v; j++) {
                graph[i][j] = Integer.MAX_VALUE;
            }
        }
        for (int i = 0; i < e; i++) {
            split=scanner.nextLine().split(" ");

            int i1 = Integer.parseInt(split[0]);
            int i2 = Integer.parseInt(split[1]);
            int i3 = Integer.parseInt(split[2]);
            graph[i1][i2]= i3;
            graph[i2][i1]= i3;
        }

        boolean[] visited = new boolean[v+1];
        int[] minTree=new int[v+1];
        Arrays.fill(minTree,Integer.MAX_VALUE-1);

        for (int i = 1; i <= v; i++){
            int node = -1;
            int minValue = Integer.MAX_VALUE;

            for (int j = 1; j <= v; j++) {
                if (!visited[j] && minTree[j]<minValue) {
                    minValue = minTree[j];
                    node = j;
                }
            }

            if (node!=1) ans+=minValue;
            visited[node]=true;
            for (int j = v; j > 0; j--) {
                if (!visited[j] && graph[node][j]<minTree[j]){
                    minTree[j]=graph[node][j];
                }
            }
        }
        System.out.println(ans);
    }
}
